﻿package 
{
	import flash.display.*;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.media.SoundLoaderContext;
	import flash.net.URLRequest;
	
	/**
	 * ...
	 * @author hato
	 */
	public class HaipaiManager extends Sprite
	{
		// 各牌のイメージを保持
		private var img_manz:Array = new Array();
		private var img_souz:Array = new Array();
		private var img_pinz:Array = new Array();
		
		// 作業イメージ
		private var sprite_ary:Array = new Array();
			
		// 読み込んだ数
		private var n_request_all:int = 0;
		private var n_request_now:int = 0;
		
		//　適当な文字を設定・保持する
		private var _tag:String;
		
		private var _hai: String;
		
		private var _tsumo: String;
		
		public function HaipaiManager():void
		{
			//
			// 表示用スプライトセット。移動しないので固定位置。
			//
			
			var X:int = 25;
			var Y:int = 250;
			for (var j:int = 0; j <= 13; j++) {
				var sprite:Sprite = new Sprite();
				
				var dummy: int = (j == 13)? 20:0;// ツモ牌分
				sprite.x = j * 30 + X + dummy;
				sprite.y = Y;
				sprite_ary[j] = sprite;
				
				// 各牌をクリックした時の処理を定義
				sprite.addEventListener(
					MouseEvent.CLICK, onSub(j));
				function onSub(j:int) :Function {
					return function (e:Event):void{;  
					//trace(e.target.name);  
					OnHaiClick(e,j);// イベント引数付き
					} ;  
				}
				this.addChild(sprite);
			}
			
		}
		private function OnHaiClick(event:Event, hai_num:int):void
		{
			//trace(hai_num);
			//trace(this.hai);
			//trace("---------------");
			
			addEventListener(MyEvent.HAI_CLICK, onHaiClick);
			
			// TEST 送出
			dispatchEvent(new MyEvent(MyEvent.HAI_CLICK, hai_num));
		}
		
		private function onHaiClick(e:MyEvent):void 
		{
			//trace("test event2");
			//trace(e.v);
		}
		
		
	
		public function LoadImages(baseurl:String):void
		{
			// 画像データ読み込み
			var h_type:Array = ["man", "sou", "pin"];
			for (var cnt:String in h_type) {
				var type:String = h_type[cnt];
				for (var i:int = 1; i <= 9;i++) {
					//var baseurl:String = "img/";
					var str:String = baseurl + type + i + "u.gif";	
					var imgloader:Loader = new Loader();
					imgloader.contentLoaderInfo.addEventListener(
						Event.COMPLETE, onSub(type, i));
					function onSub(type:String, i:int) :Function {  
						return function (e:Event):void{;  
						//trace(e.target.name);  
							
						OnImageLoadComplete(e, type, i);// イベント引数付き
						} ;  
					}
					imgloader.load(new URLRequest(str));
					
					n_request_all++;
				}
			}
			// -> OnImageLoadComplete
		}
		
		private function OnImageLoadComplete(event:Event, type:String, i:int):void 
		{
			var loader:Loader = Loader(event.target.loader);
			var bitmap:Bitmap = Bitmap(loader.content);
			//trace(bitmap);
			//trace(type + String(i));
				
			switch(type) {
				case "man":
					img_manz[i] = bitmap;
					break;
				case "sou":
					img_souz[i] = bitmap;
					break;
				case "pin":
					img_pinz[i] = bitmap;
					break;
				default:
					//trace("other");
					img_manz[i] = bitmap;
			}
			
			n_request_now++;
			if (n_request_all == n_request_now) {
				// 読み込み終了
				trace("(HaipaiManager.as) End Load Images");
				
				// COMPLETE 送出
				var event:Event = new Event( Event.COMPLETE );
				this.dispatchEvent(event);
			}
		}
		
		public function get tag():String { return _tag; }
		
		public function set tag(value:String):void 
		{
			_tag = value;
			
			var p_sprite:Sprite = sprite_ary[2];
			var p_bitmap:Bitmap;
			
			p_bitmap = img_manz[tag];
			p_sprite.addChild(p_bitmap);
		}
		
		//
		// 手牌を保持するプロパティ
		//
		public function get hai():String { return _hai; }
		
		public function set hai(value:String):void 
		{
			var ary:Array = value.split(",");
			for (var cnt:String in ary) {
				var unit:String = ary[cnt];// m:5
				var unit_ary:Array = unit.split(":");
				var type: String = unit_ary[0];// m
				var num:  String = unit_ary[1];// 5
				//trace(cnt  + type + num);
				
				var p_sprite:Sprite = sprite_ary[cnt];
				// bitmap解放ここから
				if (p_sprite.numChildren > 0) {
					//trace("sprite child object num " + p_sprite.numChildren);
					
					var child_bitmap:Bitmap = p_sprite.getChildAt(0) as Bitmap;
					if (child_bitmap != null) {
						//trace("test");
						child_bitmap.bitmapData.dispose();
						p_sprite.removeChild(child_bitmap);
						child_bitmap = null;
						
						//trace(p_sprite.getChildAt(0));
						//trace(p_sprite.contains(p_sprite.getChildAt(0)));
						//var bitmap_index:int = p_sprite.getChildIndex(child_bitmap);
						//trace("sprite contains is "      + p_sprite.contains(child_bitmap));
						//trace("sprite bitmap index "     + String(bitmap_index));
						//trace("sprite child 0 is "       + p_sprite.getChildAt(0));
					}
				}
				// ここまで
				var p_bitmap:Bitmap;
				switch (type) {
					case "m":
						p_bitmap = img_manz[num];
						break;
					case "s":
						p_bitmap = img_souz[num];
						break;
					case "p":
						p_bitmap = img_pinz[num];
						break;
					default :
						p_bitmap = img_manz[num];
				}
				
				var cp_bitmap:Bitmap = new Bitmap(p_bitmap.bitmapData.clone());
				cp_bitmap.scaleY = 2;
				cp_bitmap.scaleX = 2;
				
				p_sprite.addChild(cp_bitmap);
			}
			_hai = value;
		}
		
		public function get tsumo():String { return _tsumo; }
		
		public function set tsumo(value:String):void 
		{
			_tsumo = value;
			var unit_ary:Array = tsumo.split(":");
			var type: String = unit_ary[0];// m
			var num:  String = unit_ary[1];// 5
			
			var p_sprite:Sprite = sprite_ary[13];
			// bitmap解放ここから
			if (p_sprite.numChildren > 0) {
				//trace("sprite child object num " + p_sprite.numChildren);
				
				var child_bitmap:Bitmap = p_sprite.getChildAt(0) as Bitmap;
				if (child_bitmap != null) {
					//trace("test");
					child_bitmap.bitmapData.dispose();
					p_sprite.removeChild(child_bitmap);
					child_bitmap = null;
					
					//trace(p_sprite.getChildAt(0));
					//trace(p_sprite.contains(p_sprite.getChildAt(0)));
					//var bitmap_index:int = p_sprite.getChildIndex(child_bitmap);
					//trace("sprite contains is "      + p_sprite.contains(child_bitmap));
					//trace("sprite bitmap index "     + String(bitmap_index));
					//trace("sprite child 0 is "       + p_sprite.getChildAt(0));
				}
			}
			// ここまで
			var p_bitmap:Bitmap;
			switch (type) {
				case "m":
					p_bitmap = img_manz[num];
					break;
				case "s":
					p_bitmap = img_souz[num];
					break;
				case "p":
					p_bitmap = img_pinz[num];
					break;
				default :
					p_bitmap = img_manz[num];
			}
			
			var cp_bitmap:Bitmap = new Bitmap(p_bitmap.bitmapData.clone());
			cp_bitmap.scaleY = 2;
			cp_bitmap.scaleX = 2;
			
			p_sprite.addChild(cp_bitmap);
			
		}
		
		
	}
	
}